package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zhentao.pojo.Merchant;
import com.zhentao.pojo.Middle;
import com.zhentao.service.MerchantService;
import com.zhentao.service.MiddleService;
import com.zhentao.service.RoleService;
import com.zhentao.service.UsersService;
import com.zhentao.util.ResponseResult;
import com.zhentao.util.TextModerationPlusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/merchant")
public class ApMerchant {
    @Autowired
    RoleService roleService;
    @Autowired
    UsersService usersService;
    @Autowired
    MiddleService middleService;
    @Autowired
    MerchantService merchantService;
    @RequestMapping("findAdd")
    public String findAdd(@RequestBody Merchant merchant) {
        Middle middle = new Middle();
        ResponseResult responseResult= TextModerationPlusUtil.checkContent(merchant.getUsername());
        if(responseResult.getCode()==600){
            merchantService.save(merchant);
            middle.setMerchantId(merchant.getMerchantId());
            middle.setRoleId(merchant.getRoles());
            middleService.save(middle);
            return "商家用户添加成功";
        }else {
            return "审核失败";
        }

    }
    @RequestMapping("findAll")
    public List<Merchant> findAll(){
        List<Merchant> list = merchantService.list();
        return list;
    }
    @RequestMapping("delete")
    public String delete(Integer merchantId){
        merchantService.removeById(merchantId);
        QueryWrapper<Middle> qw=new QueryWrapper<>();
        qw.eq("merchant_Id",merchantId);
        middleService.remove(qw);
        return "删除成功";
    }
    @RequestMapping("update")
    public String update(@RequestBody Merchant merchant){
        Merchant merchant1=new Merchant();
        merchant1.setUsername(merchant.getUsername());
        merchant1.setPassword(merchant.getPassword());
        merchant1.setPhone(merchant.getPhone());
        merchantService.updateById(merchant);
        return "修改成功";
    }
}
